﻿/* Write a program that encodes and decodes a string using given encryption key (cipher). 
 * The key consists of a sequence of characters. 
 * The encoding/decoding is done by performing XOR (exclusive or) operation over the first letter of the string with the first of the key, the second – with the second, etc.
 * When the last key character is reached, the next is the first.
*/
using System;
using System.Text;

namespace _7.EncodeString
{
    class Encode
    {
        //prompta za decodiraneto e mahnat, nqkoi simvoli ne izlizat i ne moje da se kopira ot konzolata
        //v edno realno prilojenie bih go napravil da zapisva/chete ot binary file s Unicode encoding, taka shte se izbegnat problemite sys simvolite
        
        //static int? choice = null;
        static string str = "alabala"; //string to encode
        static void Main()
        {
            Console.OutputEncoding = Encoding.Unicode;
            int choice;
            do
            {
                PrintMenu();
                bool iProvided = false;
                do
                {
                    if (iProvided == true)
                    {
                        Console.WriteLine("Invalid option.");
                        PrintMenu();
                    }
                    else iProvided = true;
                } while (Int32.TryParse(Console.ReadLine(), out choice) == false || choice < 0 || choice > 2);
                switch (choice)
                {
                    case 1:
                        EncodeString();
                        break;
                    case 2:
                        DecodeString();
                        break;
                    case 0:
                        Environment.Exit(0);
                        break;
                }
            } while (choice != 0);
        }

        private static void PrintMenu()
        {
            Console.WriteLine("Please choose an option: ");
            Console.WriteLine("1. Encode");
            Console.WriteLine("2. Decode");
            Console.WriteLine("0. Exit");
            Console.Write("What are we gonna do? ");
        }

    private static void DecodeString()
    {
        //prompta za decodiraneto e mahnat, nqkoi simvoli ne izlizat i ne moje da se kopira ot konzolata

        //read encoded string
        Console.Write("Enter encoded string: ");
        //string str = "alabala";
        Console.WriteLine(str);
        //21242122212421
        //read decryption key
        Console.WriteLine("Please provide the decryption key: ");
        string deckey = Console.ReadLine();

        //store decryption string in a String Builder type
        StringBuilder decstr = new StringBuilder();

        //decode using xor
        int keyl = deckey.Length;
        int CurrentKeyPosition = 0;
        for (int i = 0; i < str.Length; i++)
        {
            decstr.Append((char)(str[i] ^ deckey[CurrentKeyPosition]));
            if (CurrentKeyPosition == keyl - 1) CurrentKeyPosition = 0;
        }
        string decoded = decstr.ToString();
        Console.WriteLine("Decoded string: {0}", decoded);
        str = decoded;

    }

    private static void EncodeString()
    {
        //prompta za decodiraneto/kodiraneto e mahnat, nqkoi simvoli ne izlizat i ne moje da se kopira ot konzolata, v edno realno prilojenie moje bi e naj-dobre da se chete ot binary file

        //read string in plain text
        Console.Write("Enter string: ");
        //string str = "alabala";
        Console.WriteLine(str);

        //read encryption key
        Console.WriteLine("Please provide an encryption key: ");
        string enckey = Console.ReadLine();
        
        //store encryption string in a String Builder type
        StringBuilder encstr = new StringBuilder();

        //encode using xor
        int keyl = enckey.Length;
        int CurrentKeyPosition = 0;
        for (int i = 0; i < str.Length; i++)
        {
            encstr.Append((char)(str[i] ^ enckey[CurrentKeyPosition]));
            if (CurrentKeyPosition == keyl - 1) CurrentKeyPosition = 0;
        }
        string encoded = encstr.ToString();
        Console.WriteLine("Encoded string: {0}", encoded);
        str = encoded;
    }
}
}
